<template>
	<view>
		<u-navbar title="切换门店" placeholder :autoBack="true" leftIconColor="#E6A5B4" bgColor="#000000"
			:titleStyle="titleStyle">
		</u-navbar>
		<view v-if="address" class="topTv">当前定位地点</view>
		<view v-if="address" class="cityTv">
			{{address}}
		</view>
		<view v-if="myShopInfo" class="itemV1">
			<view class="topV">
				<image class="topIv" :src="myShopInfo.logo" mode="aspectFill"></image>
				<view class="rightV">
					<view class="rightV2">
						<view class="textTV1">{{myShopInfo.name}}</view>
						<view class="textTV2">当前门店</view>
					</view>
					<view class="textTV3">{{myShopInfo.distance}}</view>
				</view>
			</view>
			<view class="bottomV">
				<image src="https://s1.ax1x.com/2023/04/24/p9mKJne.png"></image>
				<view>{{myShopInfo.address}}</view>
			</view>
		</view>


		<view class="topTv" style="margin-top: 30rpx;">附近健身房</view>

		<view class="itemV1" v-for="(item,index) in shopList" :key="index">
			<view class="topV">
				<image class="topIv" :src="item.logo" mode="aspectFill"></image>
				<view class="rightV">
					<view class="rightV2">
						<view class="textTV1">{{item.name}}</view>
						<view class="textTV22" @click="onItemClick(item)">选这个</view>
					</view>
					<view class="textTV3">{{item.distance}}</view>
				</view>
			</view>
			<view class="bottomV">
				<image src="https://s1.ax1x.com/2023/04/24/p9mKJne.png"></image>
				<view>{{item.address}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				titleStyle: {
					"color": "#ffffff"
				},
				myShopInfo: null,
				shopList: [],
				address:"",
			}
		},
		onLoad() {
			
			if(uni.getStorageSync("user_latitude")){
				this.getCityName();
				this.getSeleteMyShopList();
				this.getSeleteShopList();
			}else{
				this.onDing();
			}

		},
		methods: {
			onDing() {
				let that = this;
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.getLocation({
							type: 'gcj02',
							geocode: true, //设置该参数为true可直接获取经纬度及城市信息
							success: function(res) {
								
								uni.setStorageSync("user_longitude", res.longitude);
								uni.setStorageSync("user_latitude", res.latitude);
								that.getCityName();
								that.getSeleteMyShopList();
								that.getSeleteShopList();
							},
							fail(res) {
								console.log("定位失败--" + JSON.stringify(res))
								uni.$u.toast("定位失败");
							}
						});
					},
					fail() {
						uni.showModal({
							title: '未打开小程序定位',
							content: '找不到您的位置，请开启定位',
							confirmText: '开启定位',
							showCancel: false,
							success: (res) => {
								if (res.confirm) {
									uni.openSetting(); // 打开地图权限设置
								}
							}
						});
					}
				})
			},
			getCityName() {
				this.$api.getCityName({
					longitude: uni.getStorageSync("user_longitude"),
					latitude: uni.getStorageSync("user_latitude")
				}).then((res) => {
					this.address = res.address;
				}).catch((e) => {})
			},
			getSeleteMyShopList() {
				this.$api.getSeleteMyShopList({
					longitude: uni.getStorageSync("user_longitude"),
					latitude: uni.getStorageSync("user_latitude")
				}).then((res) => {
					this.myShopInfo = res.info;

				}).catch((e) => {
					uni.$u.toast("我的门店获取失败");
				})
			},
			getSeleteShopList() {
				this.$api.getSeleteShopList({
					longitude: uni.getStorageSync("user_longitude"),
					latitude: uni.getStorageSync("user_latitude")
				}).then((res) => {
					this.shopList = res.list;
				}).catch((e) => {
					uni.$u.toast("获取门店列表失败");
				})
			},
			onItemClick(item) {
				this.getSeleteShop(item.id);
			},
			getSeleteShop(id) {
				this.$api.getSeleteShop({
					shop_id: id
				}).then((res) => {
					uni.$u.toast("切换门店成功");
					setTimeout(() => {
						uni.navigateBack();
					}, 900)
				}).catch((e) => {
					uni.$u.toast("切换门店失败");
				})
			},
		}
	}
</script>

<style lang="scss">
	.topTv {
		font-size: 32rpx;
		color: #8D8D8D;
		padding-left: 30rpx;
	}

	.cityTv {
		font-size: 32rpx;
		color: #FFFFFF;
		padding-left: 30rpx;
	}

	.itemV1 {
		margin-top: 30rpx;
		background: #1F1F1F;
		padding-right: 30rpx;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
	}

	.bottomV {
		padding-left: 30rpx;
		padding-right: 30rpx;
		margin-top: 30rpx;
		display: flex;
		align-items: center;

		image {
			width: 20rpx;
			height: 20rpx;
		}

		view {
			font-size: 24rpx;
			margin-left: 10rpx;
			color: #8D8D8D;
		}
	}

	.topV {
		display: flex;
	}

	.topIv {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		background-color: antiquewhite;
		margin-left: 30rpx;
	}

	.rightV {
		margin-left: 20rpx;
		flex: 1;
	}

	.rightV2 {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.textTV1 {
		font-size: 32rpx;
		color: #FFFFFF;
	}

	.textTV2 {
		font-size: 28rpx;
		color: #FFFFFF;
		min-width: 150rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}


	.textTV22 {
		font-size: 32rpx;
		color: #CEF743;
		min-width: 160rpx;
		height: 60rpx;
		border-radius: 30rpx;
		border: 2rpx solid #CEF743;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.textTV3 {
		font-size: 24rpx;
		color: #8D8D8D;
		margin-top: 24rpx;
	}
</style>
